/*
#####################################################################
####################	   T R I B U I T 		#####################
#####################################################################

Copyright (C) 2010  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.


########################################################################
*/
package OtherDialogs;

import java.awt.*;
import java.awt.event.*;
import java.text.*;

import javax.swing.*;
import javax.swing.JFormattedTextField.AbstractFormatter;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.DefaultFormatter;
import javax.swing.text.DefaultFormatterFactory;

import GuiTools.TribuitFrame;
import GuiTools.TribuitTextField;
import Settings.Language;
import Settings.Settings;

public class UTF8Table extends TribuitFrame{
    	Language LANG;
    	TribuitFrame charFrame;
    	TribuitTextField field;
    	Settings SET;
    	JSpinner hexStartSpinner,hexEndSpinner,intStartSpinner,intEndSpinner;
    	JRadioButton intRB,hexRB;
    	JPanel buttonPanel;
    	GridBagConstraints gbc;
    	int gridyIndex,gridxIndex;
    	public UTF8Table(Settings set){
    		super("UTF-8 "+set.getLanguage().getSpecialCharacter());
    		SET=set;
    		LANG = SET.getLanguage();
			charFrame = this;
			SpinnerModel intModel = new SpinnerNumberModel(SET.getCTIntValue(),0,70000,1);
			intStartSpinner = new JSpinner(intModel);
			intStartSpinner.addChangeListener(new ChangeListener(){
				public void stateChanged(ChangeEvent arg0) {
					intEndSpinner.setValue(((Integer)intStartSpinner.getValue())+128);
				}
			});
			SpinnerModel intEndModel = new SpinnerNumberModel(SET.getCTIntValue()+128,128,70000,1);
			intEndSpinner=new JSpinner(intEndModel);
			intEndSpinner.addChangeListener(new ChangeListener(){
				public void stateChanged(ChangeEvent arg0) {
					intStartSpinner.setValue(((Integer)intEndSpinner.getValue())-128);
				}
			});
			hexStartSpinner=new JSpinner(new SpinnerNumberModel(SET.getCTHexValue(),0,70000,1));
			hexStartSpinner.addChangeListener(new ChangeListener(){
				public void stateChanged(ChangeEvent arg0) {
					hexEndSpinner.setValue(((Integer)hexStartSpinner.getValue())+128);
				}
			});
			JSpinner.DefaultEditor editor =(JSpinner.DefaultEditor)hexStartSpinner.getEditor();
		    JFormattedTextField tf = editor.getTextField();
		    tf.setFormatterFactory(new MyFormatterFactory());
			hexEndSpinner=new JSpinner(new SpinnerNumberModel(SET.getCTHexValue()+128,0,70000,1));
			hexEndSpinner.addChangeListener(new ChangeListener(){
				public void stateChanged(ChangeEvent arg0) {
					hexStartSpinner.setValue(((Integer)hexEndSpinner.getValue())-128);
				}
			});
		    JSpinner.DefaultEditor editorEnd =(JSpinner.DefaultEditor)hexEndSpinner.getEditor();
		    JFormattedTextField tfEnd = editorEnd.getTextField();
		    tfEnd.setFormatterFactory(new MyFormatterFactory());

			intRB = new JRadioButton("Integer");
			intRB.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					intStartSpinner.setEnabled(true);
					intEndSpinner.setEnabled(true);
					hexStartSpinner.setEnabled(false);
					hexEndSpinner.setEnabled(false);
				}
			});
			hexRB = new JRadioButton("Hexadecimal");
			hexRB.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					intStartSpinner.setEnabled(false);
					intEndSpinner.setEnabled(false);
					hexStartSpinner.setEnabled(true);
					hexEndSpinner.setEnabled(true);
				}
			});
			ButtonGroup bg = new ButtonGroup();
			bg.add(intRB);
			bg.add(hexRB);
			JButton setButton = new JButton("set");
			setButton.addActionListener(new SetBtnListener());
			JPanel rbPanel = new JPanel();
			rbPanel.add(intRB);
			rbPanel.add(intStartSpinner);
			rbPanel.add(new JLabel(" - "));
			rbPanel.add(intEndSpinner);
			rbPanel.add(hexRB);
			rbPanel.add(hexStartSpinner);
			rbPanel.add(new JLabel(" - "));
			rbPanel.add(hexEndSpinner);
			rbPanel.add(setButton);
			JPanel northPanel = new JPanel();
			northPanel.setLayout(new BoxLayout(northPanel,BoxLayout.Y_AXIS));
			northPanel.add(new JLabel(LANG.getCharExplaination()));
			northPanel.add(rbPanel);
			buttonPanel = new JPanel();
			buttonPanel.setLayout(new GridBagLayout());
			gbc = new GridBagConstraints();
			
			charFrame.getContentPane().add(BorderLayout.NORTH,northPanel);
			charFrame.getContentPane().add(BorderLayout.CENTER,buttonPanel);
			charFrame.showUp(200, 200);
			charFrame.addWindowListener(new WindowAdapter(){
				public void windowClosing(WindowEvent e){
					SET.setCTIntSelected(intRB.isSelected());
					SET.setCTIntValue((Integer)intStartSpinner.getValue());
					SET.setCTHexValue((Integer)hexStartSpinner.getValue());
				}
			});
			if(SET.getCTIntegerSelected())intRB.doClick();
			else hexRB.doClick();
			setButton.doClick();
    	}
    	
    	public void setPanels(int start, int end){
    		gridyIndex=0;
    		gridxIndex=0;
    		gbc.gridy=gridyIndex;
    		buttonPanel.removeAll();
    		for(int i=start;i<end;i++){
    			gbc.gridx=gridxIndex++;
    			char c = (char)i;
				JButton btn = new JButton(""+c);
				btn.setFocusable(false);
				btn.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						JButton b = (JButton)e.getSource();
						field = SET.getCurrentTextField();
						field.setText(field.getText()+b.getText());
					}
				});
				buttonPanel.add(btn,gbc);
				if(gridxIndex%16==0){
    				gbc.gridy=++gridyIndex;
    				gridxIndex=0;
    			}
			}
    		charFrame.pack();
    		charFrame.repaint();
    	}
    	
    	class SetBtnListener implements ActionListener{
	    	public void actionPerformed(ActionEvent e){
	    		int end,start;
	    		if(hexRB.isSelected()){
	    			start = (Integer) hexStartSpinner.getValue();
	    			end = (Integer) hexEndSpinner.getValue();
	    		}
	    		else{
	    			start = (Integer)intStartSpinner.getValue();
	    			end = (Integer)intEndSpinner.getValue();
	    		}
	    		setPanels(start,end);
	    	}
    	}
    	
	private static class MyFormatterFactory extends DefaultFormatterFactory {
		public AbstractFormatter getDefaultFormatter() {
			return new HexFormatter();
		}
	}

    private static class HexFormatter extends DefaultFormatter {
    	public Object stringToValue(String text) throws ParseException {
    		try {
    			return Integer.valueOf(text, 16);
    		} catch (NumberFormatException nfe) {throw new ParseException(text,0);}
    	}

    	public String valueToString(Object value) throws ParseException {
    		return Integer.toHexString( ((Integer)value).intValue()).toUpperCase();
    	}
	}

}